package cn.com.bluemoon.daps.sync.core.sync.sql;

import org.apache.commons.collections4.map.CaseInsensitiveMap;

import java.util.Map;

/**
 * 获取针对kv映射关系的map 转换器
 *
 * @author Jarod.Kong
 * @date 2021/3/16 15:57
 */
public abstract class MyCodeAndNameIntegerStringConverter {
    protected final CaseInsensitiveMap<Integer, String> CODE_AND_NAME_MAP = new CaseInsensitiveMap<Integer, String>();

    /**
     * map的kv => vk
     *
     * @param map
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> CaseInsensitiveMap<V, K> inverse(Map<K, V> map) {
        CaseInsensitiveMap<V, K> result = new CaseInsensitiveMap<V, K>(map.size());
        map.forEach((key, value) -> {
            result.put((V) value, (K) key);
        });
        return result;
    }

    protected void put(Integer code, String name) {
        CODE_AND_NAME_MAP.put(code, name);
    }

    /**
     * @return 获取针对kv映射关系的map
     */
    public CaseInsensitiveMap<Integer, String> getCodeAndName() {
        return CODE_AND_NAME_MAP;
    }

    public CaseInsensitiveMap<String, Integer> getNameAndCode() {
        return inverse(getCodeAndName());
    }

    protected Integer intDefaultValue() {
        return null;
    }

    protected String strDefaultValue() {
        return "";
    }

}
